<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>tweakui_test.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_tweak_tweakui_test.js.html">tweakui_test.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2013 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>goog.provide(&#39;goog.tweak.TweakUiTest&#39;);
<a name="line16"></a>goog.setTestOnly(&#39;goog.tweak.TweakUiTest&#39;);
<a name="line17"></a>
<a name="line18"></a>goog.require(&#39;goog.dom&#39;);
<a name="line19"></a>goog.require(&#39;goog.string&#39;);
<a name="line20"></a>goog.require(&#39;goog.testing.jsunit&#39;);
<a name="line21"></a>goog.require(&#39;goog.tweak&#39;);
<a name="line22"></a>goog.require(&#39;goog.tweak.TweakUi&#39;);
<a name="line23"></a>/** @suppress {extraRequire} needed for createRegistryEntries. */
<a name="line24"></a>goog.require(&#39;goog.tweak.testhelpers&#39;);
<a name="line25"></a>
<a name="line26"></a>var root;
<a name="line27"></a>var registry;
<a name="line28"></a>var EXPECTED_ENTRIES_COUNT = 14;
<a name="line29"></a>
<a name="line30"></a>function setUp() {
<a name="line31"></a>  root = document.getElementById(&#39;root&#39;);
<a name="line32"></a>  // Make both test cases use the same entries in order to be able to test that
<a name="line33"></a>  // having two UIs on the same page does not cause trouble.
<a name="line34"></a>  createRegistryEntries(&#39;&#39;);
<a name="line35"></a>  registry = goog.tweak.getRegistry();
<a name="line36"></a>}
<a name="line37"></a>
<a name="line38"></a>function tearDown() {
<a name="line39"></a>  goog.tweak.activeBooleanGroup_ = null;
<a name="line40"></a>  // When debugging a single test, don&#39;t clear out the DOM.
<a name="line41"></a>  if (window.location.search.indexOf(&#39;runTests&#39;) == -1) {
<a name="line42"></a>    root.innerHTML = &#39;&#39;;
<a name="line43"></a>  }
<a name="line44"></a>}
<a name="line45"></a>
<a name="line46"></a>function tearDownPage() {
<a name="line47"></a>  // When debugging a single test, don&#39;t clear out the DOM.
<a name="line48"></a>  if (window.location.search.indexOf(&#39;runTests&#39;) != -1) {
<a name="line49"></a>    return;
<a name="line50"></a>  }
<a name="line51"></a>  // Create both registries for interactive testing.
<a name="line52"></a>  createRegistryEntries(&#39;&#39;);
<a name="line53"></a>  registry = goog.tweak.getRegistry();
<a name="line54"></a>  // Add an extra tweak for testing the creation of tweaks after the UI has
<a name="line55"></a>  // already been rendered.
<a name="line56"></a>  var entryCounter = 0;
<a name="line57"></a>  goog.tweak.registerButton(&#39;CreateNewTweak&#39;, &#39;Creates a new tweak. Meant &#39; +
<a name="line58"></a>      &#39;to simulate a tweak being registered in a lazy-loaded module.&#39;,
<a name="line59"></a>      function() {
<a name="line60"></a>        goog.tweak.registerBoolean(
<a name="line61"></a>            &#39;Lazy&#39; + ++entryCounter, &#39;Lazy-loaded tweak.&#39;);
<a name="line62"></a>      });
<a name="line63"></a>  goog.tweak.registerButton(&#39;CreateNewTweakInNamespace1&#39;,
<a name="line64"></a>      &#39;Creates a new tweak within a namespace. Meant to simulate a tweak &#39; +
<a name="line65"></a>      &#39;being registered in a lazy-loaded module.&#39;, function() {
<a name="line66"></a>        goog.tweak.registerString(
<a name="line67"></a>            &#39;foo.bar.Lazy&#39; + ++entryCounter, &#39;Lazy-loaded tweak.&#39;);
<a name="line68"></a>      });
<a name="line69"></a>  goog.tweak.registerButton(&#39;CreateNewTweakInNamespace2&#39;,
<a name="line70"></a>      &#39;Creates a new tweak within a namespace. Meant to simulate a tweak &#39; +
<a name="line71"></a>      &#39;being registered in a lazy-loaded module.&#39;, function() {
<a name="line72"></a>        goog.tweak.registerNumber(
<a name="line73"></a>            &#39;foo.bar.baz.Lazy&#39; + ++entryCounter, &#39;Lazy combo&#39;, 3,
<a name="line74"></a>            {validValues: [1, 2, 3], label: &#39;Lazy!&#39;});
<a name="line75"></a>      });
<a name="line76"></a>
<a name="line77"></a>  var label = document.createElement(&#39;h3&#39;);
<a name="line78"></a>  label.innerHTML = &#39;TweakUi:&#39;;
<a name="line79"></a>  root.appendChild(label);
<a name="line80"></a>  createUi(false);
<a name="line81"></a>
<a name="line82"></a>  label = document.createElement(&#39;h3&#39;);
<a name="line83"></a>  label.innerHTML = &#39;Collapsible:&#39;;
<a name="line84"></a>  root.appendChild(label);
<a name="line85"></a>  createUi(true);
<a name="line86"></a>}
<a name="line87"></a>
<a name="line88"></a>function createUi(collapsible) {
<a name="line89"></a>  var tweakUiElem = collapsible ? goog.tweak.TweakUi.createCollapsible() :
<a name="line90"></a>      goog.tweak.TweakUi.create();
<a name="line91"></a>  root.appendChild(tweakUiElem);
<a name="line92"></a>}
<a name="line93"></a>
<a name="line94"></a>function getAllEntryDivs() {
<a name="line95"></a>  return goog.dom.getElementsByTagNameAndClass(&#39;div&#39;,
<a name="line96"></a>      goog.tweak.TweakUi.ENTRY_CSS_CLASS_);
<a name="line97"></a>}
<a name="line98"></a>
<a name="line99"></a>function getEntryDiv(entry) {
<a name="line100"></a>  var label = goog.tweak.TweakUi.getNamespacedLabel_(entry);
<a name="line101"></a>  var allDivs = getAllEntryDivs();
<a name="line102"></a>  var ret;
<a name="line103"></a>  for (var i = 0, div; div = allDivs[i]; i++) {
<a name="line104"></a>    var divText = goog.dom.getTextContent(div);
<a name="line105"></a>    if (goog.string.startsWith(divText, label) &amp;&amp;
<a name="line106"></a>        goog.string.contains(divText, entry.description)) {
<a name="line107"></a>      assertFalse(&#39;Found multiple divs matching entry &#39; + entry.getId(), !!ret);
<a name="line108"></a>      ret = div;
<a name="line109"></a>    }
<a name="line110"></a>  }
<a name="line111"></a>  assertTrue(&#39;getEntryDiv failed for &#39; + entry.getId(), !!ret);
<a name="line112"></a>  return ret;
<a name="line113"></a>}
<a name="line114"></a>
<a name="line115"></a>function getEntryInput(entry) {
<a name="line116"></a>  var div = getEntryDiv(entry);
<a name="line117"></a>  return div.getElementsByTagName(&#39;input&#39;)[0] ||
<a name="line118"></a>      div.getElementsByTagName(&#39;select&#39;)[0];
<a name="line119"></a>}
<a name="line120"></a>
<a name="line121"></a>function testCreate() {
<a name="line122"></a>  createUi(false);
<a name="line123"></a>  assertEquals(&#39;Wrong number of entry divs.&#39;, EXPECTED_ENTRIES_COUNT,
<a name="line124"></a>      getAllEntryDivs().length);
<a name="line125"></a>
<a name="line126"></a>  assertFalse(&#39;checkbox should not be checked 1&#39;,
<a name="line127"></a>      getEntryInput(boolEntry).checked);
<a name="line128"></a>  assertTrue(&#39;checkbox should be checked 2&#39;,
<a name="line129"></a>      getEntryInput(boolEntry2).checked);
<a name="line130"></a>  // Enusre custom labels are being used.
<a name="line131"></a>  var html = document.getElementsByTagName(&#39;button&#39;)[0].innerHTML;
<a name="line132"></a>  assertTrue(&#39;Button label is wrong&#39;, html.indexOf(&#39;&amp;lt;btn&amp;gt;&#39;) &gt; -1);
<a name="line133"></a>  html = getEntryDiv(numEnumEntry).innerHTML;
<a name="line134"></a>  assertTrue(&#39;Enum2 label is wrong&#39;, html.indexOf(&#39;second&amp;amp;&#39;) &gt; -1);
<a name="line135"></a>}
<a name="line136"></a>
<a name="line137"></a>function testToggleBooleanSetting() {
<a name="line138"></a>  boolEntry.setValue(true);
<a name="line139"></a>  createUi(false);
<a name="line140"></a>
<a name="line141"></a>  assertTrue(&#39;checkbox should be checked&#39;,
<a name="line142"></a>      getEntryInput(boolEntry).checked);
<a name="line143"></a>
<a name="line144"></a>  boolEntry.setValue(false);
<a name="line145"></a>  assertFalse(&#39;checkbox should not be checked 1&#39;,
<a name="line146"></a>      getEntryInput(boolEntry).checked);
<a name="line147"></a>}
<a name="line148"></a>
<a name="line149"></a>function testToggleStringSetting() {
<a name="line150"></a>  strEntry.setValue(&#39;val1&#39;);
<a name="line151"></a>  createUi(false);
<a name="line152"></a>
<a name="line153"></a>  assertEquals(&#39;Textbox has wrong value 1&#39;,
<a name="line154"></a>      &#39;val1&#39;, getEntryInput(strEntry).value);
<a name="line155"></a>
<a name="line156"></a>  strEntry.setValue(&#39;val2&#39;);
<a name="line157"></a>  assertEquals(&#39;Textbox has wrong value 2&#39;,
<a name="line158"></a>      &#39;val2&#39;, getEntryInput(strEntry).value);
<a name="line159"></a>}
<a name="line160"></a>
<a name="line161"></a>function testToggleStringEnumSetting() {
<a name="line162"></a>  strEnumEntry.setValue(&#39;B&#39;);
<a name="line163"></a>  createUi(false);
<a name="line164"></a>
<a name="line165"></a>  assertEquals(&#39;wrong value 1&#39;,
<a name="line166"></a>      &#39;B&#39;, getEntryInput(strEnumEntry).value);
<a name="line167"></a>
<a name="line168"></a>  strEnumEntry.setValue(&#39;C&#39;);
<a name="line169"></a>  assertEquals(&#39;wrong value 2&#39;,
<a name="line170"></a>      &#39;C&#39;, getEntryInput(strEnumEntry).value);
<a name="line171"></a>}
<a name="line172"></a>
<a name="line173"></a>
<a name="line174"></a>function testToggleNumericSetting() {
<a name="line175"></a>  numEntry.setValue(3);
<a name="line176"></a>  createUi(false);
<a name="line177"></a>
<a name="line178"></a>  assertEquals(&#39;wrong value 1&#39;,
<a name="line179"></a>      &#39;3&#39;, getEntryInput(numEntry).value);
<a name="line180"></a>
<a name="line181"></a>  numEntry.setValue(4);
<a name="line182"></a>  assertEquals(&#39;wrong value 2&#39;,
<a name="line183"></a>      &#39;4&#39;, getEntryInput(numEntry).value);
<a name="line184"></a>}
<a name="line185"></a>
<a name="line186"></a>function testToggleNumericEnumSetting() {
<a name="line187"></a>  numEnumEntry.setValue(2);
<a name="line188"></a>  createUi(false);
<a name="line189"></a>
<a name="line190"></a>  assertEquals(&#39;wrong value 1&#39;,
<a name="line191"></a>      &#39;2&#39;, getEntryInput(numEnumEntry).value);
<a name="line192"></a>
<a name="line193"></a>  numEnumEntry.setValue(3);
<a name="line194"></a>  assertEquals(&#39;wrong value 2&#39;,
<a name="line195"></a>      &#39;3&#39;, getEntryInput(numEnumEntry).value);
<a name="line196"></a>}
<a name="line197"></a>
<a name="line198"></a>function testClickBooleanSetting() {
<a name="line199"></a>  createUi(false);
<a name="line200"></a>
<a name="line201"></a>  var input = getEntryInput(boolEntry);
<a name="line202"></a>  input.checked = true;
<a name="line203"></a>  input.onchange();
<a name="line204"></a>  assertTrue(&#39;setting should be true&#39;, boolEntry.getNewValue());
<a name="line205"></a>  input.checked = false;
<a name="line206"></a>  input.onchange();
<a name="line207"></a>  assertFalse(&#39;setting should be false&#39;, boolEntry.getNewValue());
<a name="line208"></a>}
<a name="line209"></a>
<a name="line210"></a>function testToggleDescriptions() {
<a name="line211"></a>  createUi(false);
<a name="line212"></a>  var toggleLink = root.getElementsByTagName(&#39;a&#39;)[0];
<a name="line213"></a>  var heightBefore = root.offsetHeight;
<a name="line214"></a>  toggleLink.onclick();
<a name="line215"></a>  assertTrue(&#39;Expected div height to grow from toggle descriptions.&#39;,
<a name="line216"></a>      root.offsetHeight &gt; heightBefore);
<a name="line217"></a>  toggleLink.onclick();
<a name="line218"></a>  assertEquals(&#39;Expected div height to revert from toggle descriptions.&#39;,
<a name="line219"></a>      heightBefore, root.offsetHeight);
<a name="line220"></a>}
<a name="line221"></a>
<a name="line222"></a>function assertEntryOrder(entryId1, entryId2) {
<a name="line223"></a>  var entry1 = registry.getEntry(entryId1);
<a name="line224"></a>  var entry2 = registry.getEntry(entryId2);
<a name="line225"></a>  var div1 = getEntryDiv(entry1);
<a name="line226"></a>  var div2 = getEntryDiv(entry2);
<a name="line227"></a>  var order = goog.dom.compareNodeOrder(div1, div2);
<a name="line228"></a>  assertTrue(entry1.getId() + &#39; should be before &#39; + entry2.getId(), order &lt; 0);
<a name="line229"></a>}
<a name="line230"></a>
<a name="line231"></a>function testAddEntry() {
<a name="line232"></a>  createUi(false);
<a name="line233"></a>  goog.tweak.registerBoolean(&#39;Lazy1&#39;, &#39;Lazy-loaded tweak.&#39;);
<a name="line234"></a>  goog.tweak.registerBoolean(&#39;Lazy2&#39;, &#39;Lazy-loaded tweak.&#39;,
<a name="line235"></a>      /* defaultValue */ false, { restartRequired: false });
<a name="line236"></a>  goog.tweak.beginBooleanGroup(&#39;LazyGroup&#39;, &#39;Lazy-loaded tweak.&#39;);
<a name="line237"></a>  goog.tweak.registerBoolean(&#39;Lazy3&#39;, &#39;Lazy-loaded tweak.&#39;);
<a name="line238"></a>  goog.tweak.endBooleanGroup();
<a name="line239"></a>
<a name="line240"></a>  assertEquals(&#39;Wrong number of entry divs.&#39;, EXPECTED_ENTRIES_COUNT + 4,
<a name="line241"></a>      getAllEntryDivs().length);
<a name="line242"></a>  assertEntryOrder(&#39;Enum2&#39;, &#39;Lazy1&#39;);
<a name="line243"></a>  assertEntryOrder(&#39;Lazy1&#39;, &#39;Lazy2&#39;);
<a name="line244"></a>  assertEntryOrder(&#39;Lazy2&#39;, &#39;Num&#39;);
<a name="line245"></a>  assertEntryOrder(&#39;BoolGroup&#39;, &#39;Lazy3&#39;);
<a name="line246"></a>}
<a name="line247"></a>
<a name="line248"></a>function testAddNamespacedEntries() {
<a name="line249"></a>  createUi(false);
<a name="line250"></a>  goog.tweak.beginBooleanGroup(&#39;NS.LazyGroup&#39;, &#39;Lazy-loaded tweak.&#39;);
<a name="line251"></a>  goog.tweak.registerBoolean(&#39;NS.InGroup&#39;, &#39;Lazy-loaded tweak.&#39;);
<a name="line252"></a>  goog.tweak.endBooleanGroup();
<a name="line253"></a>  goog.tweak.registerBoolean(&#39;NS.Banana&#39;, &#39;Lazy-loaded tweak.&#39;);
<a name="line254"></a>  goog.tweak.registerBoolean(&#39;NS.Apple&#39;, &#39;Lazy-loaded tweak.&#39;);
<a name="line255"></a>
<a name="line256"></a>  assertEquals(&#39;Wrong number of entry divs.&#39;, EXPECTED_ENTRIES_COUNT + 5,
<a name="line257"></a>      getAllEntryDivs().length);
<a name="line258"></a>  assertEntryOrder(&#39;Enum2&#39;, &#39;NS.Apple&#39;);
<a name="line259"></a>  assertEntryOrder(&#39;NS.Apple&#39;, &#39;NS.Banana&#39;);
<a name="line260"></a>  assertEntryOrder(&#39;NS.Banana&#39;, &#39;NS.InGroup&#39;);
<a name="line261"></a>}
<a name="line262"></a>
<a name="line263"></a>function testCollapsibleIsLazy() {
<a name="line264"></a>  if (document.createEvent) {
<a name="line265"></a>    createUi(true);
<a name="line266"></a>    assertEquals(&#39;Expected no entry divs.&#39;, 0, getAllEntryDivs().length);
<a name="line267"></a>    var showLink = root.getElementsByTagName(&#39;a&#39;)[0];
<a name="line268"></a>    var event = document.createEvent(&#39;MouseEvents&#39;);
<a name="line269"></a>    event.initMouseEvent(&#39;click&#39;, true, true, window, 0, 0, 0, 0, 0, false,
<a name="line270"></a>        false, false, false, 0, null);
<a name="line271"></a>    showLink.dispatchEvent(event);
<a name="line272"></a>    assertEquals(&#39;Wrong number of entry divs.&#39;, EXPECTED_ENTRIES_COUNT,
<a name="line273"></a>        getAllEntryDivs().length);
<a name="line274"></a>  }
<a name="line275"></a>}
</pre>


</body>
</html>
